//
//  RealNameAuthentication.m
//  安好
//
//  Created by lianchuang on 14/11/25.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "RealNameAuthentication.h"

@implementation RealNameAuthentication
{
    NSTimer *time;
    NSInteger count;
}
- (void)KeyboardWillHiden
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)downRealNameView
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
 *  键盘下来后，当前view恢复原来位置
 *
 *  @param not 通知
 */
- (void)keyboardWillHidden:(NSNotification *)not
{
    self.backView.frame=CGRectMake(0, 64, kScreenWidth, kScreenHeight);
}
//判断手机号是否符合规则
- (BOOL)checkPhoneNumber:(NSString *)tel
{
    if ([tel length] == 0) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil) message:NSLocalizedString(@"手机号不能为空", nil) delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    NSString *regex = @"^((13[0-9])|(147)|(145)|(15[^4,\\D])|(18[0-9])|(17[6-8]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:tel];
    if (!isMatch) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }
    return YES;
}

- (IBAction)getVerificationCode:(id)sender
{
    if ([self checkPhoneNumber:self.telPhone.text]) {
        self.VerCodeButton.userInteractionEnabled=NO;
        [self.VerCodeButton setBackgroundColor:[UIColor grayColor]];
        [self getPhoneVercode];
    }
}
- (void)getPhoneVercode
{
    self.VerCodeButton.userInteractionEnabled=NO;
    NSString * postURL = [NSString stringWithFormat:@"%@docuser/sms/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{
                                 @"platform": kPlatform,
                                 @"version":kVersion,
                                 @"phone":self.telPhone.text,
                                 @"action":@"user"
                                 
                                 };
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *status=[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"status"]];
        if ([status isEqualToString:@"1"]){
            count=59;
            time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dotime) userInfo:nil repeats:YES];
            
        }else
        {
            self.VerCodeButton.userInteractionEnabled=YES;
            [self.VerCodeButton setBackgroundColor:[ClassMethodsViewController colorWithHexString:@"#ff9d9d"]];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:[responseObject valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        self.VerCodeButton.userInteractionEnabled=YES;
    }];
    
}
-(void)dotime
{
    if (count>-1) {
        self.VerCodeLabel.text=[NSString stringWithFormat: @"%ld秒重发验证码",count];
        count--;
    }else
    {
        [time invalidate];
        self.VerCodeLabel.text=@"重发验证码";
        self.VerCodeButton.userInteractionEnabled=YES;
        [self.VerCodeButton setBackgroundColor:[ClassMethodsViewController colorWithHexString:@"#ff9d9d"]];
    }
}
- (IBAction)finish:(id)sender
{
        if (self.userName.text.length>=2&&self.userName.text.length<=4) {
            
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"真实姓名（2-4）位" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return ;
        }
    [self downRealNameView];
    if (self.realDelegate&&[self.realDelegate respondsToSelector:@selector(finishRealNameAuthor)]) {
        [self.realDelegate finishRealNameAuthor];
    }
}
- (IBAction)skipClick:(UIButton *)sender
{
    [self downRealNameView];
    if (self.realDelegate&&[self.realDelegate respondsToSelector:@selector(dismissLogInController)]) {
        [self.realDelegate dismissLogInController];
    }
}
- (IBAction)realNameBack:(UIButton *)sender {
    [self downRealNameView];
    if (self.realDelegate&&[self.realDelegate respondsToSelector:@selector(dismissLogInController)]) {
        [self.realDelegate dismissLogInController];
    }
}
/**
 *  iPhone4的动画
 *
 *  @param textField 用户名和密码
 *
 *  @return 无
 */
#pragma mark  textFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (kScreenHeight == 480) {
        if (textField == self.userName || textField == self.telPhone){
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, -100, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, -140, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
        }
    }else if(kScreenHeight==568){
        if (textField == self.userName || textField == self.telPhone){
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, -100, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, -140, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];

        }
    }else if (kScreenWidth==375)
    {
        if (textField == self.userName || textField == self.telPhone){
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
            
        }
    }
    
    return YES;
}
/**
 *  iPhone4的动画
 *
 *  @param textField 用户名和密码
 *
 *  @return 无
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    if (kScreenHeight == 480) {
        if (textField == self.userName || textField == self.telPhone){
            
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, 64, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                self.backView.frame=CGRectMake(0, 64, kScreenWidth, kScreenHeight);
            } completion:^(BOOL finished) {
            }];
        }
    }else if (kScreenHeight == 568)
    {
        [UIView animateWithDuration:0.3 animations:^{
            self.backView.frame=CGRectMake(0, 64, kScreenWidth, kScreenHeight);
        } completion:^(BOOL finished) {
        }];
    }
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:self.userName]) {
        if (textField.text.length>=2&&textField.text.length<=10) {
            
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"真实姓名（2-10）位，确定后不可修改" delegate:self cancelButtonTitle:@"修改姓名" otherButtonTitles:@"取消", nil];
            [alert show];
            return ;
        }
    }
}
#pragma  mark         textFieldDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [self.userName becomeFirstResponder];
            break;
        default:
            break;
    }
}
@end
